package service

import (
	"singo/model"
	"singo/serializer"
)

// ListTaskService 需求列表服务
type ListTaskService struct {
	Limit      int    `form:"pageSize"`
	Start      int    `form:"pageNo"`
	ProjectID  string `form:"project_id"`
	UserID     string `form:"user_id"`
	Done       uint   `form:"done"`
	Priority   bool   `form:"priority"`
	StartTime  string `form:"start_time" json:"start_time"`
	EndTime    string `form:"end_time" json:"end_time"`
	DeadlineAt bool   `form:"deadline_at"`
}

// TaskList 需求列表
func (service *ListTaskService) TaskList() serializer.Response {
	tasks := []model.Task{}
	tasks2 := []model.Task{}
	total := 0
	total2 := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	var orderText string
	if service.Priority {
		orderText = "priority"
	} else if service.DeadlineAt {
		orderText = "deadline_at"
	} else if service.Done == 3 {
		orderText = "review_status, complete_at desc, created_at desc"
	} else {
		orderText = "created_at desc"
	}

	// 根据执行人相关查询
	if service.UserID != "" {
		done_total := 0
		rest_total := 0

		if service.Done == 3 {
			if err := model.DB.Model(&tasks).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.UserID+"%").Not("status", 4).Count(&total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}

			if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.UserID+"%").Where(&model.Task{Status: service.Done}).Order(orderText).Find(&tasks).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
		} else {
			if err := model.DB.Model(&tasks).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.UserID+"%").Where("status = ?", 3).Count(&done_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Model(&tasks).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.UserID+"%").Where("status in (?)", []int{1, 2}).Count(&rest_total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}

			if err := model.DB.Model(&tasks).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.UserID+"%").Not("status", 4).Count(&total).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
			if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.UserID+"%").Not("status", 3).Not("status", 4).Order(orderText).Find(&tasks).Error; err != nil {
				return serializer.Response{
					Status: 50000,
					Msg:    "数据库连接错误",
					Error:  err.Error(),
				}
			}
		}
		return serializer.BuildTaskListResponse(serializer.BuildTasks(tasks), uint(total), uint(done_total), uint(rest_total))
	} else {
		if err := model.DB.Model(&tasks).Where("project_id = ?", service.ProjectID).Not("status", 3).Not("status", 4).Count(&total).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("project_id = ?", service.ProjectID).Not("status", 3).Not("status", 4).Order("created_at desc").Find(&tasks).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		if err := model.DB.Model(&tasks2).Where("project_id = ?", service.ProjectID).Not("status", 1).Not("status", 2).Count(&total2).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}

		if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("project_id = ?", service.ProjectID).Not("status", 1).Not("status", 2).Order("status, complete_at desc").Find(&tasks2).Error; err != nil {
			return serializer.Response{
				Status: 50000,
				Msg:    "数据库连接错误",
				Error:  err.Error(),
			}
		}
		all_tasks := []model.Task{}
		all_tasks = append(all_tasks, tasks...)
		all_tasks = append(all_tasks, tasks2...)

		return serializer.BuildListResponse(serializer.BuildTasks(all_tasks), uint(total+total2))
	}
}
